<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      style="display: flex;"
      label-width="100px"
    >
    <div class="specialStyle">
      <el-form-item label="渠道编号" prop="channelCode">
        <el-input
          v-model="queryParams.channelCode"
          placeholder="请输入渠道编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="渠道名称" prop="channelName">
        <el-input
          v-model="queryParams.channelName"
          placeholder="请输入渠道名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!-- <el-form-item label="部门" prop="deptId">
        <treeselect
          style="width: 200px"
          v-model="form.deptId"
          :options="deptOptions"
          :show-count="true"
          placeholder="请选择归属部门"
        />
      </el-form-item> -->
      <el-form-item label="负责人" prop="personInCharge">
        <el-input
          v-model="queryParams.personInCharge"
          placeholder="请输入负责人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="负责人电话" prop="personInChargePhone">
        <el-input
          v-model="queryParams.personInChargePhone"
          placeholder="请输入负责人电话"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
    </div>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['genius:platform-channel:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          plain
          icon="el-icon-refresh"
          size="mini"
          @click="getList"
          v-hasPermi="['genius:platform-channel:add']"
          >刷新</el-button
        >
      </el-col>
      <!-- <el-col :span="1.5">
          <el-button
            type="success"
            plain
            icon="el-icon-edit"
            size="mini"
            :disabled="single"
            @click="handleUpdate"
            v-hasPermi="['genius:platform-channel:edit']"
          >修改</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="danger"
            plain
            icon="el-icon-delete"
            size="mini"
            :disabled="multiple"
            @click="handleDelete"
            v-hasPermi="['genius:platform-channel:removed']"
          >删除</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="warning"
            plain
            icon="el-icon-download"
            size="mini"
            @click="handleExport"
            v-hasPermi="['genius:platform-channel:export']"
          >导出</el-button>
        </el-col> -->
      <!-- <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar> -->
      <div style="float: right;color: #555555;font-size: 14px;padding-right:8px">共{{total}}条数据</div>
    </el-row>

    <el-table
      v-loading="loading"
      :data="PlatformChannelList"
      @selection-change="handleSelectionChange"
      sort-change="orderNum"
    >
      <!-- <el-table-column type="selection" width="55" align="center" /> -->
      <el-table-column label="序号" align="center" prop="id" />
      <el-table-column label="渠道编号" align="center" prop="channelCode" />
      <el-table-column label="渠道名称" align="center" prop="channelName" />
      <el-table-column label="主管部门" align="center" prop="deptName" />
      <el-table-column label="排序" align="center" prop="orderNum" />
      <el-table-column
        label="渠道负责人"
        align="center"
        prop="personInCharge"
      />
      <el-table-column
        label="负责人电话"
        align="center"
        prop="personInChargePhone"
      />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="moveUp(scope.row)"
            >上移</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="moveDown(scope.row)"
            >下移</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['genius:platform-channel:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['genius:platform-channel:removed']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改渠道对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="渠道编号" prop="channelCode">
          <el-input v-model="form.channelCode" placeholder="请输入渠道编号" />
        </el-form-item>
        <el-form-item label="渠道名称" prop="channelName">
          <el-input v-model="form.channelName" placeholder="请输入渠道名称" />
        </el-form-item>
        <el-form-item label="排序" prop="orderNum">
          <el-input v-model="form.orderNum" type="number" />
        </el-form-item>
        <el-form-item label="主管部门" prop="deptId">
          <treeselect
            v-model="form.deptId"
            :options="deptOptions"
            :show-count="true"
            placeholder="请选择归属部门"
          />
        </el-form-item>
        <el-form-item label="渠道负责人" prop="personInCharge">
          <el-input v-model="form.personInCharge" placeholder="请输入负责人" />
        </el-form-item>
        <el-form-item label="负责人电话" prop="personInChargePhone">
          <el-input
            v-model="form.personInChargePhone"
            placeholder="请输入负责人电话"
          />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listPlatformChannel,
  getPlatformChannel,
  removedPlatformChannel,
  addPlatformChannel,
  updatePlatformChannel,
} from "@/api/channel";

import { deptTreeSelect } from "@/api/system/user";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";

export default {
  name: "Platform-channel",
  components: {
    Treeselect,
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 渠道表格数据
      PlatformChannelList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        channelCode: null,
        channelName: null,
        deptId: null,
        personInCharge: null,
        personInChargePhone: null,
        removed: null,
        orderByColumn: "orderNum",
        isAsc: "asc",
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        channelCode: [
          { required: true, message: "渠道编号不能为空", trigger: "blur" },
        ],
        channelName: [
          { required: true, message: "渠道名称不能为空", trigger: "blur" },
        ],
        deptId: [{ required: true, message: "部门不能为空", trigger: "blur" }],
        personInCharge: [
          { required: true, message: "负责人不能为空", trigger: "blur" },
        ],
        orderNum: [
          { required: true, message: "排序序号不能为空", trigger: "blur" },
        ],
        personInChargePhone: [
          {
            pattern:
              /^(0|86|17951)?(13[0-9]|15[012356789]|166|17[3678]|18[0-9]|14[57])[0-9]{8}$/,
            required: true,
            message: "请正确输入电话号码",
            trigger: "blur",
          },
        ],
      },
      deptOptions: undefined,
      deptName: undefined,
    };
  },
  created() {
    this.getDeptTree();
    this.getList();
  },
  methods: {
    /** 查询部门下拉树结构 */
    getDeptTree() {
      deptTreeSelect().then((response) => {
        this.deptOptions = response.data;
        this.deptName = response.data[0].label;
      });
    },
    /** 查询渠道列表 */
    getList() {
      this.loading = true;
      listPlatformChannel(this.queryParams).then((response) => {
        this.PlatformChannelList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        channelCode: null,
        channelName: null,
        deptId: null,
        personInCharge: null,
        personInChargePhone: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null,
        removed: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加渠道";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getPlatformChannel(id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改渠道";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updatePlatformChannel(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addPlatformChannel(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除渠道编号为"' + ids + '"的数据项？')
        .then(function () {
          // 修改为逻辑删除
          return removedPlatformChannel(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    //上移
    moveUp(row) {
      let form = "";
      let form1 = "";
      this.PlatformChannelList.forEach((item, index) => {
        if (item.id == row.id) {
          if (index == 0) {
            this.$modal.msgError("已经是第一条了");
            return;
          }
          form = this.PlatformChannelList[index];
          form1 = this.PlatformChannelList[index - 1];
        }
      });
      if (form1.orderNum == form.orderNum) {
        form.orderNum -= 1;
        updatePlatformChannel(form).then((response) => {
          this.getList();
        });
        this.$modal.msgSuccess("上移成功");
      } else {
        let temp = form.orderNum;
        form.orderNum = form1.orderNum;
        form1.orderNum = temp;

        updatePlatformChannel(form).then((response) => {});
        updatePlatformChannel(form1).then((response) => {
          this.$modal.msgSuccess("上移成功");
          this.getList();
        });
      }
    },
    //下移
    moveDown(row) {
      let form = "";
      let form1 = "";
      this.PlatformChannelList.forEach((item, index) => {
        if (item.id == row.id) {
          if (index == this.PlatformChannelList.length - 1) {
            this.$modal.msgError("已经是最后一条了");
            return;
          }
          form = this.PlatformChannelList[index];
          form1 = this.PlatformChannelList[index + 1];
        }
      });
      if (form1.orderNum == form.orderNum) {
        form.orderNum += 1;
        updatePlatformChannel(form).then((response) => {
          this.getList();
        });
        this.$modal.msgSuccess("下移成功");
      } else {
        let temp = form.orderNum;
        form.orderNum = form1.orderNum;
        form1.orderNum = temp;
        updatePlatformChannel(form).then((response) => {});
        updatePlatformChannel(form1).then((response) => {
          this.$modal.msgSuccess("下移成功");
          this.getList();
        });
      }
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "genius/platform-channel/export",
        {
          ...this.queryParams,
        },
        `platform-channel_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
<style scoped>
.specialStyle ::v-deep .el-form-item__label {
  border-radius: 4px 0px 0px 4px;
  color: #5a5a5a, 100%; /* 修改文字颜色 */
  font-size: 14px; /* 修改文字大小 */
  background-color: #f6f9fc;
  text-align: center;
  padding: 0px 12px 0px 12px;
  font-family: "PingFangSC";
  font-weight: 400;
  height: 35px;
  border: 1px solid #dbdbdb;
}

.specialStyle ::v-deep .el-form-item__content {
  border-radius: 0px 4px 4px 0px;
  border: 1px solid #dbdbdb;

  /* background-color: red; */
}
.specialStyle ::v-deep .el-input--small .el-input__inner {
  border: none;
}
::v-deep .el-textarea__inner {
  height: 130px;
}

</style>
